<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;


class AuthSingle extends Model
{

    protected $table ='auth_single';
    protected $appends = ['enable', 'enableDays'];
    public function user(){
        return $this->belongsTo('App\Models\User','user_id','id');
    }

    public function getEnableAttribute()
    {
        if (!$this->endTime) {
            return false;
        }
        if ($this->endTime && $this->endTime < now()) {
            return false;
        } else {
            return true;
        }
    }

    public function getEnableDaysAttribute()
    {
        if (!$this->endTime) {
            return -1;
        }
        $res = date_diff(date_create($this->endTime), now());
        return $res->days * ($res->invert == 1 ? 1 : -1);
    }

}
